﻿using QRCoder;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using Model;
using DAL;

namespace FubiManger.UI
{
    partial class MerchantcodeForm : Smobiler.Core.Controls.MobileForm
    {
        public MerchantcodeForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        private void panclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        string codeid = "";
        private void MerchantcodeForm_Load(object sender, EventArgs e)
        {
            ms_merchant merchant = merchantDal.Querybyid(merchantid);

            //string strCode = "http://su.funbusi.com:2924?parm=" + merchantid;
            string strCode = "http://su.funbusi.com:2824?parm=" + merchantid;
            codeid = Guid.NewGuid().ToString();
            Bitmap bit= code(strCode, 5, 7, @"C:\fbimage\" + merchant.logo, 20, 7, false);
            bit.Save(@"C:\fbcode\"+ codeid + ".png");
            bvcode.Value = strCode;
        }

        public static Bitmap code(string msg, int version, int pixel, string icon_path, int icon_size, int icon_border, bool white_edge)
        {

            QRCoder.QRCodeGenerator code_generator = new QRCoder.QRCodeGenerator();

            QRCoder.QRCodeData code_data = code_generator.CreateQrCode(msg, QRCoder.QRCodeGenerator.ECCLevel.M/* 这里设置容错率的一个级别 */, true, true, QRCoder.QRCodeGenerator.EciMode.Utf8, version);

            QRCoder.QRCode code = new QRCoder.QRCode(code_data);

            //Bitmap icon = new Bitmap(icon_path);

            Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, null, icon_size, icon_border, white_edge);

            return bmp;

        }
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pansave_Press(object sender, EventArgs e)
        {
            this.Client.SaveImageToAlbum(codeid+".png", @"C:\fbcode\");
        }
    }
}